package com.hxy.controller;

import com.github.pagehelper.PageInfo;
import com.hxy.common.JsonResult;
import com.hxy.pojo.Addressbook;
import com.hxy.pojo.vo.AddressBookVo;
import com.hxy.service.AddBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author feiyu
 * 通讯录控制类
 */
@RestController
@RequestMapping("/addbook")
public class AddressBookController {
    @Autowired
    private AddBookService addBookService;

    /**
     * 1.通讯录||show全部和模糊查询
     *
     * @param page
     * @param limit
     * @param sex
     * @param stuffName
     * @param phone
     * @param branch
     * @return
     */
    @RequestMapping("/show")
    public JsonResult showAll(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                              @RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
                              @RequestParam(value = "sex", required = false, defaultValue = "") String sex,
                              @RequestParam(value = "stuffName", required = false, defaultValue = "") String stuffName,
                              @RequestParam(value = "phone", required = false, defaultValue = "") String phone,
                              @RequestParam(value = "branch", required = false, defaultValue = "") String branch) {
        page = page == null ? 1 : page;
        System.out.println(page + " 1" + limit + " 1" + stuffName + " 1" + sex + " 1" + phone + " 1" + branch);
        PageInfo<AddressBookVo> addressBookVoPageInfo = addBookService.selectPageInfo(page, limit, stuffName, sex, phone, branch);
        return new JsonResult(1, "success", addressBookVoPageInfo);
    }

    /**
     * 查寻单个用户信息   post请求
     * /addbook/findById
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public JsonResult find(@RequestParam(value = "id") Integer id) {
        System.out.println("/addbook/update" + id);
        Addressbook addressbook = addBookService.selectByPrimaryKey(id);
        return new JsonResult(0, "fail", addressbook);
    }

    /**
     * 修改通讯录
     *
     * @param addressBook
     * @return
     */
    @RequestMapping("/update")
    public JsonResult update(Addressbook addressBook) {
        System.out.println(addressBook);
        int i = addBookService.updateByPrimaryKey(addressBook);
        if (i > 0) {
            return new JsonResult(0, "success", null);
        } else {
            return new JsonResult(0, "fail", null);
        }

    }

    /**
     * 删除通讯录
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public JsonResult delete(@RequestParam(value = "id") Integer id) {
        System.out.println("/addbook/detele" + id);
        int i = addBookService.deleteByPrimaryKey(id);
        if (i > 0) {
            return new JsonResult(0, "success", null);
        } else {
            return new JsonResult(0, "fail", null);
        }

    }
    @RequestMapping("/add")
    public JsonResult add(Addressbook addbook){
        int insert = addBookService.insert(addbook);
        if (insert > 0) {
            return new JsonResult(0, "success",null);
        }
        else{
            return new JsonResult(1, "fail",null);
        }
    }
}
